<?php 
class Ri extends CI_Controller {
	public function __construct() {
		parent::__construct();
		
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->model('Jobs');
		$this->load->model('Company');
		$this->load->model('Rinvoices');
			
		$user =& Users::getInstance();
		if ($user->level < 1) {
			redirect('/');
		}
	}
	
	public function index() {
		$this->load->library('Menu');
		$menu = new Menu;
		$user =& Users::getInstance();
		$data = $menu->gen();
		$data['user'] = $user;
		$data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
		$data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);
		
		$data['invoices'] = $this->Rinvoices->getAll();
		
		$this->load->view('ri-list.php', $data);
	}
	
	public function add($jobId = 0) {
		$this->load->library('Menu');
		$menu = new Menu;
		$user =& Users::getInstance();
		$data = $menu->gen();
		$data['user'] = $user;
		$data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
		$data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);
		
		if ($jobId > 0) {
			$data['jobOrder'] = $this->Jobs->getOrderJob($jobId);
			$data['jobRoutes'] = $this->Jobs->getOrderJobs($jobId);
			$data['jobUser'] = $user->getUserInfo($data['jobOrder']->owner_id);
			$data['jobId'] = $jobId;
		} else {
			$data['jobId'] = 0;
		}
		
		if ($this->input->post('addInvoice')) {
			$this->Rinvoices->insert($this->input->post('clientid'), $this->input->post('period'), $this->input->post('startdate'), $this->input->post('remarks'), $this->input->post('routes'));
			
			if ($jobId > 0) {
				$dataDel = array("belongs" => $jobId);
				$this->db->delete('jobs', $dataDel); 
				
				$dataDel = array("gid" => $jobId);
				$this->db->delete('job_group', $dataDel);
			}
			
			redirect('/ri');
		}
		
		$this->load->view('ri-new.php', $data);
	}
	
	public function view($jobId = 0) {
		$this->load->library('Menu');
		$menu = new Menu;
		$user =& Users::getInstance();
		$data = $menu->gen();
		$data['user'] = $user;
		$data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
		$data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);
		
		$data['rinvoice'] = $this->Rinvoices->getRI($jobId);
		$data['ownerinfo'] = $user->getUserInfo($data['rinvoice']->uid, false);
		$data['invoices'] = $this->Rinvoices->countAll($jobId);
		
		$this->load->view('ri-view.php', $data);
	}
	
	public function searchuser() {
		$user =& Users::getInstance();
		
		$search = $this->input->post('term');
		
		echo json_encode($this->Rinvoices->usersList($search));
	}
	
	public function companyDelUser($uid) {
		$this->load->library('Menu');
		$menu = new Menu;
		$user =& Users::getInstance();
		$data = $menu->gen();
		$data['user'] = $user;
		$data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
		$data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);
		
		$this->Company->deleteUser($uid);
		
		redirect('/companies/');
	}
	
	public function editCompany($cid) {
		$this->load->library('Menu');
		$menu = new Menu;
		$user =& Users::getInstance();
		$data = $menu->gen();
		$data['user'] = $user;
		$data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
		$data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);
		
		if ($this->input->post('doedit')) {
			$this->Company->editCompany($cid, $this->input->post('name'));
		}
		
		$data['company'] = $this->Company->getCompany($cid);
		$data['formPost'] = '/companies/edit/'.$data['company']->id;
		$data['isEdit'] = true;
		
		$this->load->view('company-edit.php', $data);
	}
	
	public function newCompany() {
		$this->load->library('Menu');
		$menu = new Menu;
		$user =& Users::getInstance();
		$data = $menu->gen();
		$data['user'] = $user;
		$data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
		$data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);
		
		if ($this->input->post('doedit')) {
			$this->Company->addCompany($this->input->post('name'));
			
			redirect('/companies');
		}
		
		$data['formPost'] = '/companies/new';
		$data['isEdit'] = false;
		
		$this->load->view('company-edit.php', $data);
	}
	
	// Users
	public function users() {
		$this->load->library('Menu');
		$menu = new Menu;
		$user =& Users::getInstance();
		$data = $menu->gen();
		$data['user'] = $user;
		$data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
		$data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);
		
		$search = $this->input->post('search');
		if (empty($search)) {
			$data['users'] = $user->getAll();
		} else {
			$data['users'] = $user->search($search);
		}
		
		$this->load->view('users-list.php', $data);
	}
	
	public function editUser($uid) {
		$this->load->library('Menu');
		$menu = new Menu;
		$user =& Users::getInstance();
		$data = $menu->gen();
		$data['user'] = $user;
		$data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
		$data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);
		
		if ($this->input->post('doedit')) {
			$user->adminEdit($uid, $this->input->post('email'), $this->input->post('password'), $this->input->post('confirmpassword'), $this->input->post('name'), $this->input->post('address'), $this->input->post('phone'), $this->input->post('level'), $this->input->post('user_id'), $this->input->post('company'));
		}
		
		$data['uinfo'] = $user->getUserInfo($uid);
		$data['formPost'] = '/users/edit/'.$data['uinfo']->id;
		$data['isEdit'] = true;
		$data['companies'] = $this->Company->getAll();
		
		$this->load->view('user-edit.php', $data);
	}
	
	public function newUser() {
		$this->load->library('Menu');
		$menu = new Menu;
		$user =& Users::getInstance();
		$data = $menu->gen();
		$data['user'] = $user;
		$data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
		$data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);
		
		if ($this->input->post('doedit')) {
			$user->adminAdd($this->input->post('email'), $this->input->post('password'), $this->input->post('confirmpassword'), $this->input->post('name'), $this->input->post('address'), $this->input->post('phone'), $this->input->post('level'), $this->input->post('user_id'), $this->input->post('company'));
			
			redirect('/users');
		}
		
		$data['formPost'] = '/users/new';
		$data['isEdit'] = false;
		$data['companies'] = $this->Company->getAll();
		
		$this->load->view('user-edit.php', $data);
	}
}
?>